package qz_rpt

class OrderStatusController {

    def dynamicQueryService
    def exportFileService
    def orderStatus() {
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 100
        params.startno = (pageSize * (pageCurrent - 1)) + 1
        params.endno = pageSize * pageCurrent
        def pageSizeList = [10, 20, 50, 100]

        def orderStatus=[]
        def totalCount =0
        def tableHeadList = []
        def orderStatusAnalysisStartedList=[]
        def orderStatusSumQty=[]
        if(params.EXPORT_CONTENT_NAME=='Excel'){
            params.SYS_QUERY_NAME = 'orderStatus'
            orderStatus = dynamicQueryService.queryForList(params)
            tableHeadList = []
            if (orderStatus.size() > 0) {
                tableHeadList = orderStatus[0].keySet()
            }
            params.SYS_QUERY_NAME = 'orderStatusCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
            params.SYS_QUERY_NAME = 'orderStatusSumQty'
            orderStatusSumQty = dynamicQueryService.queryForList(params)
        }

        params.SYS_QUERY_NAME = 'orderStatusAnalysisStartedList'
        orderStatusAnalysisStartedList = dynamicQueryService.queryForList(params)

        print(tableHeadList)
        [pageSize     : pageSize, totalCount: totalCount, pageSizeList: pageSizeList,pageCurrent:pageCurrent,
         orderStatus : orderStatus, totalCount: totalCount,
         tableHeadList: tableHeadList,
         startno: params.startno ?: '', endno: params.endno ?: '', docId: params.docId ?: '',
         partName: params.partName ?: '',partDesc: params.partDesc ?: '',group3: params.group3 ?: '',
         /*orderStatusAnalysisSum:orderStatusAnalysisSum,*/orderStatusAnalysisStartedList:orderStatusAnalysisStartedList,
         startedState:params.startedState?:'',startTime:params.startTime?:'',endTime:params.endTime?:'',
         start:params.start?:'',end:params.end?:'',orderStatusSumQty:orderStatusSumQty]
    }

    def orderStatusStep(){
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 10
        params.startno = (pageSize * (pageCurrent - 1)) + 1
        params.endno = pageSize * pageCurrent
        def pageSizeList = [10, 20, 50, 100]
        print(params.state1)
        def orderStatusStep=[]
        def totalCount =0
        def tableHeadList = []
        def orderStatusStepNameList=[]
        def orderStatusStepSumQty=[]
        if(params.state1=='WAIT'||params.state1=='RUN'||params.state1=='REPAIR'||params.state1=='OUTSRC'){
            params.SYS_QUERY_NAME = 'orderStatusStep'
            orderStatusStep = dynamicQueryService.queryForList(params)
            tableHeadList = []
            if (orderStatusStep.size() > 0) {
                tableHeadList = orderStatusStep[0].keySet()
            }
            params.SYS_QUERY_NAME = 'orderStatusStepCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
            params.SYS_QUERY_NAME = 'orderStatusStepNameList'
            orderStatusStepNameList = dynamicQueryService.queryForList(params)
            params.SYS_QUERY_NAME = 'orderStatusStepSumQty'
            orderStatusStepSumQty = dynamicQueryService.queryForList(params)

        }else if(params.state1=='HOLD'){
            params.SYS_QUERY_NAME = 'orderStatusStepHold'
            orderStatusStep = dynamicQueryService.queryForList(params)
            tableHeadList = []
            if (orderStatusStep.size() > 0) {
                tableHeadList = orderStatusStep[0].keySet()

            }
            params.SYS_QUERY_NAME = 'orderStatusStepHoldCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
            params.SYS_QUERY_NAME = 'orderStatusStepNameListHold'
            orderStatusStepNameList = dynamicQueryService.queryForList(params)
            params.SYS_QUERY_NAME = 'orderStatusStepHoldSumQty'
            orderStatusStepSumQty = dynamicQueryService.queryForList(params)
        }else if(params.state1=='SCRAP'){
            params.SYS_QUERY_NAME = 'orderStatusStepScrap'
            orderStatusStep = dynamicQueryService.queryForList(params)
            tableHeadList = []
            if (orderStatusStep.size() > 0) {
                tableHeadList = orderStatusStep[0].keySet()
            }
            params.SYS_QUERY_NAME = 'orderStatusStepScrapCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
            params.SYS_QUERY_NAME = 'orderStatusStepNameListScrap'
            orderStatusStepNameList = dynamicQueryService.queryForList(params)
            params.SYS_QUERY_NAME = 'orderStatusStepScrapSumQty'
            orderStatusStepSumQty = dynamicQueryService.queryForList(params)
        }else if(params.state1=='FIN' ||params.state1=='SHIP'){
            params.SYS_QUERY_NAME = 'orderStatusStepFin'
            orderStatusStep = dynamicQueryService.queryForList(params)
            tableHeadList = []
            if (orderStatusStep.size() > 0) {
                tableHeadList = orderStatusStep[0].keySet()
            }
            params.SYS_QUERY_NAME = 'orderStatusStepFinCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
            params.SYS_QUERY_NAME = 'orderStatusStepNameListFin'
            orderStatusStepNameList = dynamicQueryService.queryForList(params)
            params.SYS_QUERY_NAME = 'orderStatusStepFinSumQty'
            orderStatusStepSumQty = dynamicQueryService.queryForList(params)
        }





        [pageSize     : pageSize, totalCount: totalCount, pageSizeList: pageSizeList,pageCurrent:pageCurrent,
         orderStatusStep : orderStatusStep, totalCount: totalCount,
         tableHeadList: tableHeadList,
         startno: params.startno ?: '', endno: params.endno ?: '', docId1: params.docId1 ?: '',
         partName1: params.partName1 ?: '',partDesc: params.partDesc ?: '',group3: params.group3 ?: '',
         step:params.step?:'',orderStatusStepNameList:orderStatusStepNameList,stepName1:params.stepName1?:'',
                state1:params.state1?:'',lineNum1:params.lineNum1?:'',
         startedState:params.startedState?:'',startTime:params.startTime?:'',endTime:params.endTime?:'',
         start:params.start?:'',end:params.end?:'',orderStatusStepSumQty:orderStatusStepSumQty]
    }


    def orderStatusStepTwo(){

        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 10
        params.startno = (pageSize * (pageCurrent - 1)) + 1
        params.endno = pageSize * pageCurrent
        def pageSizeList = [10, 20, 50, 100]
        def orderStatusStepTwo=[]
        def totalCount =0
        def tableHeadList = []
        def orderStatusStepTwoLot=[]
        def orderStatusStepTwoSumQty=[]
        if(params.group3==null){
            println("123")
        }else if(params.group3==''){
            print('234')
        }


        if(params.state2=='WAIT'||params.state2=='RUN'||params.state2=='REPAIR'||params.state2=='OUTSRC'){
            params.SYS_QUERY_NAME = 'orderStatusStepTwo'
            orderStatusStepTwo = dynamicQueryService.queryForList(params)
            tableHeadList = []
            if (orderStatusStepTwo.size() > 0) {
                tableHeadList = orderStatusStepTwo[0].keySet()
            }
            params.SYS_QUERY_NAME = 'orderStatusStepTwoCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
            params.SYS_QUERY_NAME = 'orderStatusStepTwoSumQty'
            orderStatusStepTwoSumQty = dynamicQueryService.queryForList(params)

           /* params.SYS_QUERY_NAME = 'orderStatusStepTwoLot'
            orderStatusStepTwoLot = dynamicQueryService.queryForList(params)*/
        }else if(params.state2=='HOLD'){
            params.SYS_QUERY_NAME = 'orderStatusStepTwoHold'
            orderStatusStepTwo = dynamicQueryService.queryForList(params)
            tableHeadList = []
            if (orderStatusStepTwo.size() > 0) {
                tableHeadList = orderStatusStepTwo[0].keySet()
            }
            params.SYS_QUERY_NAME = 'orderStatusStepTwoHoldCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
            params.SYS_QUERY_NAME = 'orderStatusStepTwoHoldSumQty'
            orderStatusStepTwoSumQty = dynamicQueryService.queryForList(params)
        }else if(params.state2=='SCRAP'){
            params.SYS_QUERY_NAME = 'orderStatusStepTwoScrap'
            orderStatusStepTwo = dynamicQueryService.queryForList(params)
            tableHeadList = []
            if (orderStatusStepTwo.size() > 0) {
                tableHeadList = orderStatusStepTwo[0].keySet()
            }
            params.SYS_QUERY_NAME = 'orderStatusStepTwoScrapCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
            params.SYS_QUERY_NAME = 'orderStatusStepTwoScrapSumQty'
            orderStatusStepTwoSumQty = dynamicQueryService.queryForList(params)
        }else if(params.state2=='FIN' ||params.state2=='SHIP'){
            params.SYS_QUERY_NAME = 'orderStatusStepTwoFin'
            orderStatusStepTwo = dynamicQueryService.queryForList(params)
            tableHeadList = []
            if (orderStatusStepTwo.size() > 0) {
                tableHeadList = orderStatusStepTwo[0].keySet()
            }

            params.SYS_QUERY_NAME = 'orderStatusStepTwoFinCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
            params.SYS_QUERY_NAME = 'orderStatusStepTwoFinSumQty'
            orderStatusStepTwoSumQty = dynamicQueryService.queryForList(params)
        }

        [pageSize     : pageSize, totalCount: totalCount, pageSizeList: pageSizeList,pageCurrent:pageCurrent,
         orderStatusStepTwo : orderStatusStepTwo, totalCount: totalCount,
         tableHeadList: tableHeadList,
         startno: params.startno ?: '', endno: params.endno ?: '', docId2: params.docId2 ?: '',
         partName2: params.partName2 ?: '',partDesc: params.partDesc ?: '',group3: params.group3 ?: '',
         stepName2:params.stepName2?:'',orderStatusStepTwoLot:orderStatusStepTwoLot,stepName1:params.stepName1?:'',
         state2:params.state2?:'',lineNum2:params.lineNum2?:'',lotId:params.lotId?:'',
         startedState:params.startedState?:'',startTime:params.startTime?:'',endTime:params.endTime?:'',
         start:params.start?:'',end:params.end?:'',orderStatusStepTwoSumQty:orderStatusStepTwoSumQty]
    }
    def forFeeding(){
        println(params.docId1)
        println(params.lineNum1)
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 10
        params.startno = (pageSize * (pageCurrent - 1)) + 1
        params.endno = pageSize * pageCurrent
        def tableHeadList=[]
        def pageSizeList = [10,20,50,100]
        def totalCount =0
        params.SYS_QUERY_NAME = 'forFeeding'
        def forFeeding = dynamicQueryService.queryForList(params)
        if (forFeeding.size() > 0) {
                tableHeadList = forFeeding[0].keySet()
        }


        [startno:params.startno?:'',
         endno:params.endno?:'',
         pageSize:pageSize,
         totalCount: totalCount,
         pageSizeList: pageSizeList,
         tableHeadList:tableHeadList,
         forFeeding:forFeeding,
        ]
    }

    def exportExcel() {
        def sysQueryName


        sysQueryName = params.SYS_QUERY_NAME

        params.startno = 1
        params.endno = 99999999999999999



        params.SYS_QUERY_NAME = sysQueryName

        def dataList = dynamicQueryService.queryForList(params)

        def tableHeadList =  []
        if(dataList.size()>0){
            tableHeadList =  dataList[0].keySet()
        }
        print(dataList)
        String filename = new String("${sysQueryName}.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(template: "export${params.EXPORT_CONTENT_NAME}", model: [dataList: dataList,tableHeadList:tableHeadList,startno:params.startno?:'',endno:params.endno?:''])
    }
}
